import logging
import time
import os

class MyLog():

    def __init__(self):
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)

        sh = logging.StreamHandler()
        log_path = r"C:\TT\习知\12\my_test_demo_12\异常等内容\作业\log\{}.log".format(time.strftime("%Y-%m-%d"))
        if os.path.exists(log_path) is False:
            os.makedirs(os.path.split(log_path)[0])
        fh = logging.FileHandler(filename=log_path, encoding="utf-8")
        fh.setLevel(logging.INFO)

        fm = logging.Formatter("%(asctime)s|%(levelname)s|%(filename)s|%(lineno)s|%(message)s")

        sh.setFormatter(fm)
        fh.setFormatter(fm)

        self.logger.addHandler(fh)
        self.logger.addHandler(sh)

logger = MyLog().logger